15. 练习 — 使用变量 (2/2)

练习 — 使用变量 (2/2)

QUIZ QUESTION: :

你已经知道你可以将字符串赋值给变量。但是,你还可以将各种其他数据赋值给变量,例如列表或整数。

你尚未见过下面的部分语句,但是请猜测下,哪些语句是有效的赋值语句?

ANSWER CHOICES:



赋值语句

有效或无效?

有效

无效

有效

无效

有效

SOLUTION:

赋值语句

有效或无效?

有效

有效

有效

无效

无效

有效

有效

有效

无效

无效

有效

有效

有效

QUESTION:

创建一个叫做 sides 的变量,并将列表 [1, 2, 3, 4, 5] 赋值给该变量。

SOLUTION:

NOTE: The solutions are expressed in RegEx pattern. Udacity uses these patterns to check the given answer

QUESTION:

创建一个叫做 angle 的变量,并将整数 72 赋值给该变量。

SOLUTION:

NOTE: The solutions are expressed in RegEx pattern. Udacity uses these patterns to check the given answer

QUIZ QUESTION: :

下面又是我们的紫色五边形代码:

mary.color("purple")
for side in [1, 2, 3, 4, 5]:
    mary.forward(100)
    mary.right(72)

下面哪些代码的效果将和上方代码一样?

ANSWER CHOICES:



Code

相同或不同结果

相同

相同

不同

SOLUTION:

Code

相同或不同结果

相同

相同

相同

相同

不同

Task Description:

现在该你了!在下面的 workspace 中,你将找到上个页面的代码(用于绘制紫色五边形)。请不要直接使用 100 72 [1, 2, 3, 4, 5] ,而是更改代码,将这些值赋值给变量,然后使用这些变量告诉 turtle 如何绘制图形。

如果遇到问题了,可以在 workspace 下方找到我们的解决方案。

Task List:

Task Feedback:

很棒!

Workspace

This section contains either a workspace (it can be a Jupyter Notebook workspace or an online code editor work space, etc.) and it cannot be automatically downloaded to be generated here. Please access the classroom with your account and manually download the workspace to your local machine. Note that for some courses, Udacity upload the workspace files onto https://github.com/udacity , so you may be able to download them there.

Workspace Information:

  • Default file path:
  • Workspace type: html-live
  • Opened files (when workspace is loaded): n/a

备注 :如果你无法打开上面的workspace,请去 这里


## ⚠️ 剧透! **下面是我们的解决方案。**如果你能认真完成练习,然后再将你的代码与我们的代码进行对比,学习效果将更好!

----

解决方案

注意,你可以为变量选择其他名称。

color = "purple"
sides = [1, 2, 3, 4, 5]
angle = 72
distance = 100
mary.color(color)
for side in sides:
    mary.forward(distance)
    mary.right(angle)